﻿using DevExpress.XtraLayout.Utils;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class BookingDetailForm : DevExpress.XtraEditors.XtraForm
    {
        private string m_BookingId;
        private string m_CustomerId;
        public BookingDetailForm(string bookingId)
        {
            InitializeComponent();

            this.m_BookingId = bookingId;
        }

        #region 航班信息

        private int colIndexFLIGHT_ID = 0;
        private int colIndexFLIGHT_BOOKING_ID = 0;
        private int colIndexAIRLINE_ID = 0;
        private int colIndexFLIGHT_NO = 0;
        private int colIndexDEPARTURE_DATE = 0;
        private int colIndexDEPARTURE_TIME = 0;
        private int colIndexARRIVAL_DATE = 0;
        private int colIndexARRIVAL_TIME = 0;
        private int colIndexDEPARTURE_CITY_ID = 0;
        private int colIndexARRIVAL_CITY_ID = 0;
        private List<BookingFlightModel> m_dtFlight = new List<BookingFlightModel>();
        private void ClearRecordToGrid()
        {
            this.gridView1.BeginUpdate();
            this.gridView1.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexFLIGHT_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "Id";
            c.Caption = "行程编码";
            c.ColumnEdit = this.repositoryItemSearchLookUpEdit1;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView1.Columns.Add(c);

            this.colIndexAIRLINE_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "AirlineId";
            c.Caption = "航空公司";
            c.ColumnEdit = this.repositoryItemSearchLookUpEdit1;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexFLIGHT_NO = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "FlightNo";
            c.Caption = "航班号";
            c.ColumnEdit = this.repositoryItemTextEdit1;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexDEPARTURE_TIME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "DepartureTime";
            c.Caption = "出发时间";
            c.ColumnEdit = this.repositoryItemTimeEdit1;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexARRIVAL_TIME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ArrivalTime";
            c.Caption = "到达时间";
            c.ColumnEdit = this.repositoryItemTimeEdit2;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexDEPARTURE_CITY_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "DepartureCityId";
            c.Caption = "出发城市";
            c.ColumnEdit = this.repositoryItemSearchLookUpEdit2;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexARRIVAL_CITY_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ArrivalCityId";
            c.Caption = "到达城市";
            c.ColumnEdit = this.repositoryItemSearchLookUpEdit3;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.gridView1.EndUpdate();
        }
        private void LoadFlight()
        {
            this.gridControl1.DataSource = this.m_dtFlight;
            this.gridView1.BestFitColumns();
        }

        #endregion 航班信息

        #region 乘机人信息

        private int colIndexPASSENGER_NAME = 0;
        private int colIndexMOBILE_NUMBER = 0;
        private int colIndexCUSTOMER_ID = 0;
        private int colIndexPASSENGER_ID = 0;
        private int colIndexCHINESE_SPELL = 0;
        private int colIndexID_TYPE_NAME = 0;
        private int colIndexID_TYPE_ID = 0;
        private int colIndexID_NUMBER = 0;
        private int colIndexEXPIRY_DATE = 0;
        private List<PassengerModel> m_dtPassenger = new List<PassengerModel>();
        private void ClearRecordToGrid2()
        {
            this.gridView2.BeginUpdate();
            this.gridView2.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexPASSENGER_NAME = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PassengerName";
            c.Caption = "乘机人姓名";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexMOBILE_NUMBER = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "MobileNumber";
            c.Caption = "手机号码";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexCUSTOMER_ID = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "CustomerId";
            c.Caption = "客户编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView2.Columns.Add(c);

            this.colIndexPASSENGER_ID = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "PassengerId";
            c.Caption = "乘机人编码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = false;
            this.gridView2.Columns.Add(c);

            this.colIndexCHINESE_SPELL = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ChineseSpell";
            c.Caption = "全拼";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexID_TYPE_ID = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "IDTypeId";
            c.Caption = "证件类型";
            c.ColumnEdit = this.repositoryItemLookUpEdit1;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexID_NUMBER = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "IDNumber";
            c.Caption = "证件号码";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.colIndexEXPIRY_DATE = this.gridView2.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "ExpiryDate";
            c.Caption = "有效期";
            c.ColumnEdit = this.repositoryItemDateEdit3;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "Birthday";
            c.Caption = "出生日期";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "NationId";
            c.Caption = "签证国家";
            c.ColumnEdit = this.repositoryItemLookUpEdit2;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView2.Columns.Add(c);

            this.gridView2.EndUpdate();
        }
        private void LoadPassenger()
        {
            this.gridControl2.DataSource = this.m_dtPassenger;
            this.gridView2.BestFitColumns();
        }

        #endregion 乘机人信息

        private DataTable dtAirline = new DataTable();
        private DataTable dtCity = new DataTable();
        private List<PassengerModel> lstPassenger = new List<PassengerModel>();
        private List<IDTypeModel> lstIDType = new List<IDTypeModel>();
        private List<NationModel> lstNation = new List<NationModel>();
        private bool m_Loading = false;
        private ManageState m_ManageState;
        private void BuildAll()
        {
            int visibleIndex = 0;

            this.repositoryItemSearchLookUpEdit1.View.Columns.Clear();
            this.repositoryItemSearchLookUpEdit1.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRLINE_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit1.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "NATION_NAME", Caption = "国家", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit1.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRLINE_NAME", Caption = "航空公司", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit1.DisplayMember = "AIRLINE_NAME";
            this.repositoryItemSearchLookUpEdit1.ValueMember = "AIRLINE_ID";
            this.repositoryItemSearchLookUpEdit1.DataSource = this.dtAirline;

            visibleIndex = 0;
            this.repositoryItemSearchLookUpEdit2.View.Columns.Clear();
            this.repositoryItemSearchLookUpEdit2.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit2.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_NAME", Caption = "城市", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit2.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRPORT_NAME", Caption = "机场", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit2.DisplayMember = "CITY_NAME";
            this.repositoryItemSearchLookUpEdit2.ValueMember = "CITY_ID";
            this.repositoryItemSearchLookUpEdit2.DataSource = this.dtCity;

            visibleIndex = 0;
            this.repositoryItemSearchLookUpEdit3.View.Columns.Clear();
            this.repositoryItemSearchLookUpEdit3.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_ID", Caption = "代码", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit3.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "CITY_NAME", Caption = "城市", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit3.View.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "AIRPORT_NAME", Caption = "机场", Visible = true, VisibleIndex = visibleIndex++ });
            this.repositoryItemSearchLookUpEdit3.DisplayMember = "CITY_NAME";
            this.repositoryItemSearchLookUpEdit3.ValueMember = "CITY_ID";
            this.repositoryItemSearchLookUpEdit3.DataSource = this.dtCity;

            this.clbPassenger.Items.Clear();
            this.clbPassenger.DisplayMember = "PassengerName";
            this.clbPassenger.ValueMember = "PassengerId";
            this.clbPassenger.DataSource = this.lstPassenger;

            this.repositoryItemLookUpEdit1.Columns.Clear();
            this.repositoryItemLookUpEdit1.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] { new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Id", "编码"), new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "类型") });
            this.repositoryItemLookUpEdit1.DisplayMember = "Name";
            this.repositoryItemLookUpEdit1.ValueMember = "Id";
            this.repositoryItemLookUpEdit1.DataSource = this.lstIDType;

            this.repositoryItemLookUpEdit2.Columns.Clear();
            this.repositoryItemLookUpEdit2.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] { new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Id", "编码"), new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "国家") });
            this.repositoryItemLookUpEdit2.DisplayMember = "Name";
            this.repositoryItemLookUpEdit2.ValueMember = "Id";
            this.repositoryItemLookUpEdit2.DataSource = this.lstNation;
        }
        private bool LoadBooking()
        {
            BookingModel booking = BookingDAL.Instance.Get(this.m_BookingId);
            if (booking == null)
            {
                return false;
            }
            this.m_CustomerId = booking.CustomerId;

            this.txtBookingId.Text = booking.Id;
            this.txtBookingStatus.Text = this.GetBookingStatus(booking.BookingStatus);
            this.txtBookingType.Text = this.GetBookingType(booking.BookingType);
            this.txtCustomerName.Text = booking.CustomerName;
            this.txtMobileNumber.Text = booking.MobileNumber;
            this.txtTelephone.Text = booking.Telephone;
            this.txtEMail.Text = booking.EMail;
            this.chkClaim.Checked = booking.Claim;
            this.txtContactAddress.Text = booking.ContactAddress;
            this.txtContactAddress.Enabled = booking.Claim;
            this.txtRemark.Text = booking.Remark;
            this.mmData.Text = booking.Data;
            this.txtPaymentType.Text = booking.PaymentType;
            this.txtPrintCompany.Text = booking.PrintCompany;

            this.txtSaleStaff.Text = booking.SaleStaffName;
            this.txtSaleTime.Text = booking.SaleTime.ToString("yyyy-MM-dd HH:mm:ss");
            this.txtTotalAmount.EditValue = booking.TotalAmount;
            this.txtTotalBase.EditValue = booking.TotalBase;

            this.chkPaid.Checked = booking.Paid;
            this.txtTotalPaid.EditValue = booking.TotalPaid;
            this.txtCollectionStaff.Text = booking.CollectionStaffName;
            this.txtCollectionTime.Text = booking.CollectionTime == null ? string.Empty : ((DateTime)booking.CollectionTime).ToString("yyyy-MM-dd HH:mm:ss");
            this.txtTotalPaid.Enabled = this.txtCollectionStaff.Enabled = this.txtCollectionTime.Enabled = booking.Paid;

            this.chkPrinted.Checked = booking.Printed;
            this.txtPrintStaff.Text = booking.PrintStaffName;
            this.txtPrintTime.Text = booking.PrintTime == null ? string.Empty : ((DateTime)booking.PrintTime).ToString("yyyy-MM-dd HH:mm:ss");
            this.txtPrintStaff.Enabled = this.txtPrintTime.Enabled = booking.Printed;

            this.m_dtFlight = BookingFlightDAL.Instance.Gets(booking.Id);
            List<BookingPassengerModel> passengers = BookingPassengerDAL.Instance.Gets(booking.Id);

            this.dtAirline = AirlineDAL.Instance.GetDataSet().Tables[0];
            this.dtCity = CityDAL.Instance.GetDataSet().Tables[0];
            this.lstPassenger = PassengerDAL.Instance.Gets(this.m_CustomerId);
            this.lstIDType = IDTypeDAL.Instance.Gets(true);
            this.lstNation = NationDAL.Instance.Gets();

            this.BuildAll();
            this.LoadFlight();
            this.LoadPassenger();

            this.m_dtPassenger.Clear();
            //this.m_dtPassenger = (from item1 in passengers from item2 in this.lstPassenger where item1.PassengerName == item2.PassengerName select item2).ToList();
            passengers.ForEach(t =>
            {
                int index = this.m_dtPassenger.FindIndex(m => { return string.Equals(m.IDNumber, t.IDNumber); });
                if (index == -1)
                {
                    // 不存在
                    PassengerModel model = new PassengerModel()
                    {
                        CustomerId = t.Id,
                        PassengerId = Guid.NewGuid().ToString("N").ToUpper(),
                        PassengerName = t.PassengerName,
                        ChineseSpell = t.ChineseSpell,
                        MobileNumber = t.MobileNumber,
                        IDTypeId = t.IDTypeId,
                        IDNumber = t.IDNumber,
                        ExpiryDate = t.ExpiryDate,
                        LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id,
                        LastModifiedDate = DateTime.Now,
                        Birthday = t.Birthday,
                        NationId = t.NationId,
                        Sex = t.Sex
                    };
                    this.AddPassengerToList(model);
                }
            });

            return true;
        }
        private void AddPassengerToList(PassengerModel model)
        {
            int index = this.lstPassenger.FindIndex(t => { return string.Equals(t.PassengerName, model.PassengerName) && string.Equals(t.IDNumber, model.IDNumber); });

            if (index != -1) model = this.lstPassenger[index];
            else this.lstPassenger.Add(model);

            index = this.clbPassenger.FindItem(model);
            if (index >= 0)
            {
                this.clbPassenger.SetItemChecked(index, true);
            }
            this.m_dtPassenger.Add(model);
            this.gridView2.RefreshData();
            this.gridView2.BestFitColumns();
        }

        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (Global.Instance.MandatoryFieldEmpty(this.txtCustomerName, false, ref message))
            {
                this.errorProvider1.SetError(this.txtCustomerName, message);
                this.txtCustomerName.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtMobileNumber, false, ref message))
            {
                this.errorProvider1.SetError(this.txtMobileNumber, message);
                this.txtMobileNumber.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtTotalAmount, false, ref message))
            {
                this.errorProvider1.SetError(this.txtTotalAmount, message);
                this.txtTotalAmount.Focus();
                return (false);
            }
            if (Global.Instance.MandatoryFieldEmpty(this.txtTotalBase, false, ref message))
            {
                this.errorProvider1.SetError(this.txtTotalBase, message);
                this.txtTotalBase.Focus();
                return (false);
            }
            if (this.chkClaim.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.txtContactAddress, false, ref message))
                {
                    this.errorProvider1.SetError(this.txtContactAddress, message);
                    this.txtContactAddress.Focus();
                    return (false);
                }
            }
            if (this.m_dtFlight.Count == 0)
            {
                Global.Instance.ErrorDlg("航班信息不能为空");
                this.btnAppendFlight.Focus();
                return (false);
            }
            if (this.m_dtPassenger.Count == 0)
            {
                Global.Instance.ErrorDlg("乘机人信息不能为空");
                this.btnAppendPassenger.Focus();
                return (false);
            }

            return (true);
        }
        private string GetBookingStatus(int status)
        {
            switch (status)
            {
                case 0: return "新建";
                case 10: return "未支付出票中";
                case 11: return "已支付出票中";
                case 20: return "待支付已出票";
                case 21: return "已支付已出票";
                case 30: return "已退票";
                case 31: return "部分退票";
                case 99: return "已取消";
                default: return "未知";
            }
        }
        private string GetBookingType(string type)
        {
            switch (type)
            {
                case "LOC": return "现场预订";
                case "TEL": return "电话预订";
                case "WEB": return "网上预订";
                default: return "网上预订";
            }
        }
        /// <summary>
        /// 设置界面状态
        /// </summary>
        /// <param name="state"></param>
        private void SetManageState(ManageState state)
        {
            this.m_ManageState = state;

            this.errorProvider1.ClearErrors();

            switch (state)
            {
                case ManageState.Modify:
                    this.btnModify.Enabled = false;
                    this.btnSave.Enabled = true;
                    this.btnCancel.Enabled = true;
                    this.btnCancel.Text = "取消(&C)";

                    this.txtBookingId.ReadOnly = true;
                    this.txtBookingStatus.ReadOnly = true;
                    this.txtBookingType.ReadOnly = true;
                    this.txtCustomerName.ReadOnly = false;
                    this.txtMobileNumber.ReadOnly = false;
                    this.txtTelephone.ReadOnly = false;
                    this.txtEMail.ReadOnly = false;
                    this.chkClaim.Enabled = true;
                    this.txtContactAddress.ReadOnly = false;
                    this.txtRemark.ReadOnly = false;
                    this.txtSaleStaff.ReadOnly = true;
                    this.txtSaleTime.ReadOnly = true;
                    this.txtTotalAmount.ReadOnly = false;
                    this.txtTotalBase.ReadOnly = false;
                    this.chkPaid.Enabled = false;
                    this.txtTotalPaid.ReadOnly = true;
                    this.txtCollectionStaff.ReadOnly = true;
                    this.txtCollectionTime.ReadOnly = true;
                    this.chkPrinted.Enabled = false;
                    this.txtPrintStaff.ReadOnly = true;
                    this.txtPrintTime.ReadOnly = true;
                    this.mmData.ReadOnly = true;
                    this.btnPasteData.Enabled = false;
                    this.txtPaymentType.ReadOnly = false;
                    this.txtPrintCompany.ReadOnly = false;

                    this.gridControl1.Enabled = true;
                    this.btnAppendFlight.Enabled = true;
                    this.btnDeleteFlight.Enabled = true;

                    this.clbPassenger.Enabled = true;
                    this.gridControl2.Enabled = true;
                    this.btnAppendPassenger.Enabled = true;
                    this.btnDeletePassenger.Enabled = true;
                    break;
                case ManageState.Save:
                    this.btnModify.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = false;

                    this.txtBookingId.ReadOnly = true;
                    this.txtBookingStatus.ReadOnly = true;
                    this.txtBookingType.ReadOnly = true;
                    this.txtCustomerName.ReadOnly = true;
                    this.txtMobileNumber.ReadOnly = true;
                    this.txtTelephone.ReadOnly = true;
                    this.txtEMail.ReadOnly = true;
                    this.chkClaim.Enabled = false;
                    this.txtContactAddress.ReadOnly = true;
                    this.txtRemark.ReadOnly = true;
                    this.txtSaleStaff.ReadOnly = true;
                    this.txtSaleTime.ReadOnly = true;
                    this.txtTotalAmount.ReadOnly = true;
                    this.txtTotalBase.ReadOnly = true;
                    this.chkPaid.Enabled = false;
                    this.txtTotalPaid.ReadOnly = true;
                    this.txtCollectionStaff.ReadOnly = true;
                    this.txtCollectionTime.ReadOnly = true;
                    this.chkPrinted.Enabled = false;
                    this.txtPrintStaff.ReadOnly = true;
                    this.txtPrintTime.ReadOnly = true;
                    this.mmData.ReadOnly = true;
                    this.btnPasteData.Enabled = false;
                    this.txtPaymentType.ReadOnly = true;
                    this.txtPrintCompany.ReadOnly = true;

                    this.gridControl1.Enabled = false;
                    this.btnAppendFlight.Enabled = false;
                    this.btnDeleteFlight.Enabled = false;

                    this.clbPassenger.Enabled = false;
                    this.gridControl2.Enabled = false;
                    this.btnAppendPassenger.Enabled = false;
                    this.btnDeletePassenger.Enabled = false;

                    break;
                case ManageState.Browse:
                case ManageState.Cancel:
                    this.btnModify.Enabled = true;
                    this.btnSave.Enabled = false;
                    this.btnCancel.Enabled = true;
                    this.btnCancel.Text = "关闭(&C)";

                    this.txtBookingId.ReadOnly = true;
                    this.txtBookingStatus.ReadOnly = true;
                    this.txtBookingType.ReadOnly = true;
                    this.txtCustomerName.ReadOnly = true;
                    this.txtMobileNumber.ReadOnly = true;
                    this.txtTelephone.ReadOnly = true;
                    this.txtEMail.ReadOnly = true;
                    this.chkClaim.Enabled = false;
                    this.txtContactAddress.ReadOnly = true;
                    this.txtRemark.ReadOnly = true;
                    this.txtSaleStaff.ReadOnly = true;
                    this.txtSaleTime.ReadOnly = true;
                    this.txtTotalAmount.ReadOnly = true;
                    this.txtTotalBase.ReadOnly = true;
                    this.chkPaid.Enabled = false;
                    this.txtTotalPaid.ReadOnly = true;
                    this.txtCollectionStaff.ReadOnly = true;
                    this.txtCollectionTime.ReadOnly = true;
                    this.chkPrinted.Enabled = false;
                    this.txtPrintStaff.ReadOnly = true;
                    this.txtPrintTime.ReadOnly = true;
                    this.mmData.ReadOnly = true;
                    this.btnPasteData.Enabled = false;
                    this.txtPaymentType.ReadOnly = true;
                    this.txtPrintCompany.ReadOnly = true;

                    this.gridControl1.Enabled = false;
                    this.btnAppendFlight.Enabled = false;
                    this.btnDeleteFlight.Enabled = false;

                    this.clbPassenger.Enabled = false;
                    this.gridControl2.Enabled = false;
                    this.btnAppendPassenger.Enabled = false;
                    this.btnDeletePassenger.Enabled = false;

                    break;
            }
        }

        private void BookingDetailForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.m_Loading = true;
                this.SetManageState(ManageState.Browse);

                this.ClearRecordToGrid();
                this.ClearRecordToGrid2();
                if (!this.LoadBooking())
                {
                    Global.Instance.ErrorDlg("加载订单信息失败");
                    this.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                this.m_Loading = false;
            }
        }

        private void btnAppendFlight_Click(object sender, EventArgs e)
        {
            try
            {
                using (FlightDetailForm dlg = new FlightDetailForm(this.dtAirline, this.dtCity))
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        this.m_dtFlight.Add(dlg.Data);
                        this.gridView1.RefreshData();
                        this.gridView1.BestFitColumns();
                    }
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnDeleteFlight_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gridView1.SelectedRowsCount == 0)
                {
                    Global.Instance.InfoDlg("没有可选的记录！");
                    return;
                }
                this.gridView1.DeleteSelectedRows();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
        private void clbPassenger_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            try
            {
                if (m_Loading) return;

                PassengerModel model = this.clbPassenger.GetItem(e.Index) as PassengerModel;
                if (e.State == CheckState.Checked)
                {
                    this.m_dtPassenger.Add(model);
                    this.gridView2.RefreshData();
                    this.gridView2.BestFitColumns();
                }
                else
                {
                    if (this.m_dtPassenger.Remove(model))
                    {
                        this.gridView2.RefreshData();
                        this.gridView2.BestFitColumns();
                    }
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void chkClaim_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (m_Loading) return;

                this.txtContactAddress.ReadOnly = !this.chkClaim.Checked;
                if (this.txtContactAddress.Enabled)
                {
                    this.txtContactAddress.Focus();
                    this.txtContactAddress.SelectAll();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnAppendPassenger_Click(object sender, EventArgs e)
        {
            try
            {
                using (PassengerDetailForm dlg = new PassengerDetailForm(this.m_CustomerId, this.lstIDType, this.lstNation))
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            this.m_Loading = true;

                            PassengerModel model = dlg.Data;
                            this.lstPassenger.Add(model);
                            int index = this.clbPassenger.FindItem(model);
                            if (index >= 0)
                            {
                                this.clbPassenger.SetItemChecked(index, true);
                            }
                            this.m_dtPassenger.Add(model);
                            this.gridView2.RefreshData();
                            this.gridView2.BestFitColumns();
                        }
                        finally
                        {
                            this.m_Loading = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnDeletePassenger_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gridView2.SelectedRowsCount == 0)
                {
                    Global.Instance.InfoDlg("没有可选的记录！");
                    return;
                }
                try
                {
                    this.m_Loading = true;

                    foreach (int r in this.gridView2.GetSelectedRows())
                    {
                        if (r < 0) continue;
                        PassengerModel model = this.gridView2.GetRow(r) as PassengerModel;
                        if (model != null)
                        {
                            int index = this.clbPassenger.FindItem(model);
                            if (index >= 0)
                            {
                                this.clbPassenger.SetItemChecked(index, false);
                            }
                        }
                    }
                    this.gridView2.DeleteSelectedRows();
                    this.gridView2.RefreshData();
                    this.gridView2.BestFitColumns();
                }
                finally
                {
                    this.m_Loading = false;
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetManageState(ManageState.Modify);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                Global.Instance.BeginWait();

                DateTime now = DateTime.Now;
                decimal seq = SequenceBLL.Instance.GetSeqNextValue("WS_BKG_ID_SEQ");
                string id = string.Concat(now.ToString("yyyyMMdd"), seq.ToString().PadLeft(8, '0'));

                using (DbConnection conn = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        if (conn.State != ConnectionState.Open)
                            conn.Open();

                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            BookingModel booking = new BookingModel() { Id = this.txtBookingId.Text, CustomerId = this.m_CustomerId, CustomerName = this.txtCustomerName.Text.Trim(), MobileNumber = this.txtMobileNumber.Text.Trim(), Telephone = this.txtTelephone.Text.Trim(), EMail = this.txtEMail.Text.Trim(), TotalAmount = Convert.ToDecimal(this.txtTotalAmount.EditValue), TotalBase = Convert.ToDecimal(this.txtTotalBase.EditValue), Claim = this.chkClaim.Checked, ContactAddress = this.txtContactAddress.Text.Trim(), Remark = this.txtRemark.Text, Data = this.mmData.Text, PaymentType = this.txtPaymentType.Text, PrintCompany = this.txtPrintCompany.Text, LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id, LastModifiedDate = now };

                            BookingDAL.Instance.Update(tran, booking);

                            BookingFlightDAL.Instance.Delete(tran, booking.Id);
                            foreach (BookingFlightModel flight in this.m_dtFlight)
                            {
                                flight.BookingId = id;
                                flight.LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id;
                                flight.LastModifiedDate = now;
                                BookingFlightDAL.Instance.Insert(tran, flight);
                            }
                            BookingPassengerDAL.Instance.Delete(tran, booking.Id);
                            foreach (PassengerModel item in this.m_dtPassenger)
                            {
                                BookingPassengerModel passenger = new BookingPassengerModel() { Id = Guid.NewGuid().ToString("N").ToUpper(), BookingId = id, PassengerName = item.PassengerName, ChineseSpell = item.ChineseSpell, MobileNumber = item.MobileNumber, IDTypeId = item.IDTypeId, IDNumber = item.IDNumber, ExpiryDate = item.ExpiryDate, LastModifiedStaffId = LoginInfo.Instance.StaffLogon.Staff.Id, LastModifiedDate = now, Birthday = item.Birthday, NationId = item.NationId, Sex = item.Sex };
                                BookingPassengerDAL.Instance.Insert(tran, passenger);
                            }

                            tran.Commit();

                            this.SetManageState(ManageState.Browse);
                            Global.Instance.InfoDlg("修改成功");
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();

                            Global.Instance.ErrorDlg(string.Format("修改失败 - {0}", ex.Message));
                        }
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.m_ManageState == ManageState.Append || this.m_ManageState == ManageState.Modify)
                {
                    this.SetManageState(ManageState.Cancel);
                    this.errorProvider1.ClearErrors();
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnPasteData_Click(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}
